package com.designoutput.ui
{
	import com.designoutput.elements.ImageElement;
	import com.designoutput.enum.Direction;
	import com.designoutput.service.GalleryService;
	import com.designoutput.ui.gallery.GalleryGrid;
	import com.designoutput.ui.gallery.GalleryNavigation;
	import com.designoutput.ui.gallery.GallerySearchBar;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.ui.newControls.Scrollbar;
	import com.designoutput.ui.newControls.VScroller;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.GalleryImageVo;
	import com.designoutput.vo.GalleryVo;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

	/**
	 * The galleries area, contains the search bar, galleries navigation and gallery body.
	 */
	public class GalleryCanvas extends ControlBase
	{
		public var ed:DesignoutputCard;

		private var _searchBar:GallerySearchBar;
		private var _navigationPanel:GalleryNavigation;
		private var _navigationPanelScroller:VScroller;
		private var _galleryGrid:GalleryGrid;
		
		private var _galleriesArr:Array;
		private var _tempImage:ImageElement;// recive the image element fromt the layout when edit or add new element.
		
		public function GalleryCanvas()
		{
			ed = Globals.designoutputCard;
		}
		
		override protected function initialize():void {
			_galleriesArr = [];
			_tempImage = null;
			
			_navigationPanel = new GalleryNavigation(this);
			
			_navigationPanelScroller = new VScroller();
			_navigationPanelScroller.setContent(_navigationPanel);
			addChild(_navigationPanelScroller);
			
			_galleryGrid = new GalleryGrid(this);
			addChild(_galleryGrid);
			
			_searchBar = new GallerySearchBar(this);
			addChild(_searchBar);
			
			GalleryService.instance.getGalleries(getGalleries_onComplete);
			GalleryService.instance.getGalleriesWords(getGalleriesWords_onComplete);
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			graphics.clear();
			graphics.lineStyle(2, 0, 1);
			graphics.drawRect(2, 0, 250, _height - 2);
			graphics.beginFill(0xD3D6DB, 0.7);
			graphics.drawRect(2, 0, 250, _height - 2);
			graphics.endFill();
			
			_searchBar.x = 10;
			_searchBar.y = 13;
			
			_navigationPanelScroller.x = 10;
			_navigationPanelScroller.y = 50;
			_navigationPanelScroller.width = 238;
			_navigationPanelScroller.height = _height - _navigationPanelScroller.y - 4; 
			
			_galleryGrid.x = 260;
			_galleryGrid.y = 0;
			_galleryGrid.width = _width - _galleryGrid.x - 2;
			_galleryGrid.height = _height - 2;
		}
		
		protected function getGalleries_onComplete(result:Object):void
		{
			var data:XML = new XML(result);
			parseData(data);
		}
		
		protected function getGalleriesWords_onComplete(result:Object):void
		{
			var data:XML = new XML(result);
			_searchBar.dataLoaded(data);
		}
		
		public function searchGallery(word:String=""):void{
			GalleryService.instance.searchGalleries(word, searchGalleries_onComplete);
		}
		
		protected function searchGalleries_onComplete(result:Object):void
		{
			var data:XML = new XML(result);
			parseData(data);
		}

		public function parseData(data:XML):void
		{
			_galleriesArr = [];
			var arrGalleries:XMLList=data.gallery;
			var galleriesNum:int=arrGalleries.length();
			
			for (var i:int=0; i < galleriesNum; i++)
			{
				var node:XML = arrGalleries[i];
				var galleryVo:GalleryVo = GalleryVo.parseFromXML(node);
				_galleriesArr.push(galleryVo);
			}

			_navigationPanel.buildNavList(_galleriesArr);
			_navigationPanelScroller.updateContentSize();
				
			loadGallery();
		}
		
		public function loadGallery(index:Number=0):void
		{
			var vo:GalleryVo = this._galleriesArr[index];
			_galleryGrid.setGalleryVo(vo);
		}

		public function chooseImage(elem:ImageElement):void
		{
			_tempImage = elem;
		}

		public function onChangeImage(imageVo:GalleryImageVo):void
		{
			if (_tempImage){
				trace("for ImageElement");
				_tempImage.loadImage(imageVo.url);
			}else{
				trace("for Card bg");
				ed.card.setBackgroundImage(imageVo.url);
			}
			ed.showLayout();
		}
	}
}